/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion;

import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;
import java.util.Map;

import javolution.util.FastMap;

import com.aionemu.commons.network.mmocore.ReceivablePacket;
import com.aionemu.commons.network.mmocore.SelectorConfig;
import com.aionemu.commons.network.mmocore.SelectorThread;
import com.aionemu.gameserver.utils.ThreadPoolManager;

public final class GameSelectorThread extends SelectorThread<GameClient>
{
	private static final class SingletonHolder
	{
		private static final GameSelectorThread INSTANCE;
		
		static
		{
			final SelectorConfig sc = new SelectorConfig();
			sc.setSelectorSleepTime(5);
			
			try
			{
				INSTANCE = new GameSelectorThread(sc, new AionPacketHandlerFactory());
			}
			catch (Exception e)
			{
				throw new Error(e);
			}
		}
	}
	
	public static GameSelectorThread getInstance()
	{
		return SingletonHolder.INSTANCE;
	}
	
	private GameSelectorThread(SelectorConfig sc, AionPacketHandlerFactory packetHandler)
		throws IOException
	{
		super(sc, packetHandler);
	}
	
	// ==============================================
	
	@Override
	protected GameClient createClient(SocketChannel socketChannel) throws ClosedChannelException
	{
		return new GameClient(this, socketChannel);
	}
	
	// ==============================================
	
	private final Map<String, Integer> _legalConnections = new FastMap<String, Integer>().shared();
	
	@Override
	public boolean acceptConnectionFrom(SocketChannel sc)
	{
		if (!super.acceptConnectionFrom(sc))
			return false;
		
		/*
		final String ip = sc.socket().getInetAddress().getHostAddress();
		
		System.out.println(ip);
		final Integer count = _legalConnections.get(ip);
		
		if (count == null)
			return false;
		
		if (count == 1)
			_legalConnections.remove(ip);
		else
			_legalConnections.put(ip, count - 1);
			*/
		return true;
	}
	
	public void legalize(String ip)
	{
		final Integer count = _legalConnections.get(ip);
		
		if (count == null)
			_legalConnections.put(ip, 1);
		else
			_legalConnections.put(ip, count + 1);
	}
	
	@Override
	public boolean canReceivePacketFrom(GameClient client, int opcode)
	{
		if (!super.canReceivePacketFrom(client, opcode))
			return false;
		
		if (client.isDisconnected())
			return false;
		
		return true;
	}

	/* (non-Javadoc)
	 * @see com.aionemu.commons.network.mmocore.SelectorThread#executePacket(com.aionemu.commons.network.mmocore.ReceivablePacket)
	 */
	@Override
	protected void executePacket(ReceivablePacket<GameClient> packet)
	{
		ThreadPoolManager.getInstance().execute(packet);
	}
}
